package cn.maihe.elg.operation.centers.guogu.auth;

import cn.maihe.elg.operation.centers.guogu.config.GuoGuCenterInfosConfig;
import cn.maihe.elg.operation.common.auth.Encryption;
import cn.maihe.elg.operation.config.ElgAuthConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ggy.encrypttools.sm.SMUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @Description SM4
 * @Author SSX
 * @Date 2025/03/18
 */
@Slf4j
public class GuoGuEncryption implements Encryption {

    private final String sm2PubKey;
    private final String sm2PriKey;

    private final ObjectMapper objectMapper = new ObjectMapper();
    private final ElgAuthConfig elgAuthInfo;
    private final GuoGuCenterInfosConfig.CenterInfo centerInfo;

    public GuoGuEncryption(ElgAuthConfig elgAuthConfig, GuoGuCenterInfosConfig.CenterInfo centerInfo) {
        this.elgAuthInfo = elgAuthConfig;
        this.centerInfo = centerInfo;

        this.sm2PriKey = centerInfo.getSm2PriKey();
        this.sm2PubKey = centerInfo.getSm2PubKey();
    }


    /**
     * 先sm2加密后再base64编码
     *
     * @param clearBizReqBody 明文
     * @return
     */
    @Override
    public byte[] encrypt(byte[] clearBizReqBody) {
        return encrypt(new String(clearBizReqBody, StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8);
    }

    /**
     *  先sm2加密后再base64编码
     * @param clearBizReqBody 明文
     * @return
     */
    public String encrypt(String clearBizReqBody) {
        Assert.hasText(clearBizReqBody, () -> "待加密数据不能为空");
        try {
            //return SM2Util.encrypt(sm2PubKey, clearBizReqBody);
            return SMUtils.encryptBySM2(clearBizReqBody,sm2PubKey);
        } catch (Exception e) {
            throw new RuntimeException("加密异常:" + e.getMessage(), e);
        }
    }

    /**
     *  先sm2加密后再base64编码
     * @param clearBizReqBody 明文
     * @return
     */
    public String encryptForGuoGu(String clientId,String clearBizReqBody) {
        Assert.hasText(clearBizReqBody, () -> "待加密数据不能为空");
        try {
            Map<String, GuoGuCenterInfosConfig.ClientInfo> clientInfos = centerInfo.getClientInfos();
            GuoGuCenterInfosConfig.ClientInfo clientInfo = clientInfos.get(clientId);
            return SMUtils.encryptBySM2(clearBizReqBody,clientInfo.getClientPubKey());
            //return SMUtils.encryptBySM2(clearBizReqBody,secretKey);
        } catch (Exception e) {
            throw new RuntimeException("加密异常:" + e.getMessage(), e);
        }
    }


    /**
     * 平台的私钥解密
     *
     * @param encryptedData
     * @return
     */
    @Override
    public byte[] decrypt(byte[] encryptedData) {
        return decrypt(new String(encryptedData, StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8);
    }

    /**
     *  先base64解码后再sm2解密
     * @param encryptAndB64ReqBody sm2加密后base64编码字符串
     * @return
     */
    public String decrypt(String encryptAndB64ReqBody) {
        Assert.hasText(encryptAndB64ReqBody, () -> "待解密数据不能为空");
        try {
            //return SM2Util.decrypt(sm2PriKey, encryptAndB64ReqBody);
            return SMUtils.decryptBySM2(encryptAndB64ReqBody, sm2PriKey);
        } catch (Exception e) {
            throw new RuntimeException("解密失败:" + e.getMessage(), e);
        }
    }

}
